Part 50: Assignment #23: Pollution-Sensing Window
Pollution-Sensing WindowNot a bad idea, since I haven't seen the air quality monitor on my tablet go above 'Poor' (well, it was 'Moderate' ONE day) since I got here. You'd probably need a screen on the inside so people wouldn't put their fingers in the path of the window as it opened and closed, but that's a hardware design issue.
"Average of 50" sounds a bit daunting given that MCs can't divide, but there's a little shortcut that I can take here. Since it's always an average of the past eight time units, that works out to:
(sum of readings) / 8 ≥ 50
Except, that can also be written as,
sum / 8 * 8 ≥ 50 * 8
and then
sum ≥ 400.
So I just sum the past eight readings each time unit, and only open the window if they're less than 400! Seems simple. This is definitely one for the single-MC-rides-again train.
[ ~ ]
And there we go! ¥7, and 1.3K (yikes) average power.
The numbers look big, but I'm not sure at this point in time how I can do any better.
The MC starts by adding the latest value to the memory chip, using the chip's d0 data line. It then gets the address of the most recent reading (a0), moves the other memory pointer (a1) back 8 cells, and sums everything up (which ends with the two pointers equal to each other because of the auto-increment). Then it opens or closes the window.
I've added as many optimizations as I can think of - the oldest reading gets moved directly to acc, instead of moving 0 there and adding it as part of the loop. The loop doesn't keep a counter or stop address, because it can check if the memory cell's address pointers (a0 and a1) are pointing at the same place to know when to stop. All of these save a little power.
(Before I optimized, I was sitting at 1.5K power instead of 1.3K!)
An easy job, overall.
Wait, what's a sliding window algorithm?
...   ...   ...
Aw, shiiiiiiiiiiiiiiiiit.
[ ~ ~ ]
OK, I was able to make a revision to the design and upload it on the grounds that it was a 'slight improvement' before Jie sent it anywhere. Thank god.
Here's the new version:
It's still ¥7, but the power usage is back to normal: 277 on average, per run.
This time, the MC uses the 'sliding window concept' to keep a running total of the sum of the last 8 time units (by adding the most recent value and also storing it, and subtracting the one from 9 time units ago) in the MC's acc. Checking whether it's 400 or more is still the same, but the rest is a lot simpler.
Aaaand we're done! I think that'll be all for today. 😅 It works, and now it works well.
I think, in this job, that's all I can really ask for.